<?php

namespace app\controller;

use app\model\User;
use support\Db;
use support\Request;
use Webman\Captcha\CaptchaBuilder;
use yzh52521\hash\Hash;


/**
 * 后台用户控制器
 */
class UserController extends BaseController
{

    /**
     * 输出验证码图像
     */
    public function login(Request $request)
    {
//var_dump($request->post('username'));
        $user = User::where('user_name',$request->post('username'))->first();
        if(empty($user)){
            return json($this->error('用户名活密码错误'));
        }
        //return $this->success('操作成功');
       // echo $user->password;
        if(Hash::check($request->post('password'), $user->password)){
            return json($this->success('操作成功')) ;
        }
        return json($this->error('用户名活密码错误'));

    }

    /**
     * 检查验证码
     */
    public function check(Request $request)
    {
        // 获取post请求中的captcha字段
        $captcha = $request->post('captcha');
        // 对比session中的captcha值
        if (strtolower($captcha) !== $request->session()->get('captcha')) {
            return json(['code' => 400, 'msg' => '输入的验证码不正确']);
        }
        return json(['code' => 0, 'msg' => 'ok']);
    }

    public function index(Request $request)
    {
        echo 44;
       // Db::ta
        $users = Db::table('movie_basic')->get();
        return   json(['code' => 0, 'msg' => $users]);
    }

    public function view(Request $request)
    {
        return view('index/view', ['name' => 'webman']);
    }

    public function json(Request $request)
    {
        return json(['code' => 0, 'msg' => 'ok']);
    }

}
